/**
 * 资源信息数据列表页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("AUTHORITY_RESOURCE_INFO", "AUTHORITY");

    //重置数据列表宽度
    $("#gridTableWidgetBody").resize(function(){
        $("#gridTable").setGridWidth($(this).width(), false);
    });

    $("#insert").click(function(){
        openInsertPage("");
    });

    //加载数据列表
    loadGrid();
});

/**
* 加载数据列表
*/
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype:"POST",
        url: springUrl+"/web/resourceInfo/treeGirdQuery.json",
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel:[
            {name:'id', label:'ID', width:50, sortable: false, resizable:false, hidden: true},
            {name:'resourceName',label:'资源名称', width:200, sortable: false, align:'left'},
            {name:'resourceCode',label:'资源编码', width:100, sortable: false, align:'left'},
            {name:'resourceUrl',label:'资源URL', width:100, sortable: false, align:'left'},
            {name:'resourceSort',label:'资源排序', width:100, sortable: false, align:'center'},
            {name:'resourceType',label:'资源类型', width:100, sortable: false, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue == 1){
                        return "目录";
                    }else{
                        return "节点";
                    }
                }
            },
            {name:'resourceIcon',label:'图标', width:100, sortable: false, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(rowObject.resourceType == 1 && rowObject.parentResourceCode == "ROOT"){
                        return "<i class='fa fa-"+cellValue+" blue'></i>";
                    }else if(rowObject.resourceType == 2) {
                        return "<i class='fa fa-"+cellValue+" blue'></i>";
                    }else{
                        return "";
                    }
                }
            },
            {name:'createTime',label:'创建日期', width:100, sortable: false, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue != null){
                        return new Date(cellValue).format("yyyy-MM-dd hh:mm:ss");
                    }else{
                        return "";
                    }
                }
            },
            {name:'', label:'操作', index:'', width:230, fixed:true, sortable:false, resize:false, align:'right',
                formatter:function(cellValue,options,rowObject){
                    var context = "";
                    if(rowObject.resourceType == 1){
                        context += '<button class="btn btn-white btn-info btn-bold" onclick="openInsertPage('+rowObject.id+')"><i class="ace-icon fa fa-plus-circle bigger-120 blue"></i>新增</button> ';
                    }
                    context += '<button class="btn btn-white btn-info btn-bold" onclick="openUpdatePage('+rowObject.id+')"><i class="ace-icon fa fa-pencil bigger-120 blue"></i>修改</button> ';
                    context += '<button class="btn btn-white btn-info btn-bold" onclick="deleteByPrimaryKey('+rowObject.id+')"><i class="ace-icon fa fa-trash-o bigger-120 blue"></i>删除</button>';
                    return context;
                }
            }
        ],
        treeGrid: true,
        treeGridModel: 'adjacency', //treeGrid模式，跟json元数据有关
        ExpandColumn : 'resourceName',
        treeIcons:{plus:'ace-icon fa fa-folder bigger-120',minus:'ace-icon fa fa-folder-open bigger-120',leaf:'ace-icon fa fa-book bigger-120'},
        tree_root_level: 0,
        //scroll: "true",
        rowNum:1000,
        pager : "#gridPager",
        viewrecords : false,
        pgbuttons: false,
        pgtext: "",
        pginput: false,
        jsonReader: {
            repeatitems: false,
            root: "rows"
        },
        loadComplete : function() {
            $(this).setGridWidth($("#gridTableWidgetBody").width(), true);
        }
    });

    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false, addicon : 'ace-icon fa fa-plus-circle purple',
            edit: false, editicon : 'ace-icon fa fa-pencil blue',
            del: false, delicon : 'ace-icon fa fa-trash-o red',
            view: false, viewicon : 'ace-icon fa fa-search-plus grey',
            search: false, searchicon : 'ace-icon fa fa-search orange',
            refresh: true,
            refreshtitle:"刷新",
            refreshicon : 'ace-icon fa fa-refresh green'
        }
    );
    //gridTable.navSeparatorAdd("#gridPager",{sepclass : "ui-separator", sepcontent: ''});
    //gridTable.navButtonAdd('#gridPager',{
    //    caption:"",
    //    title:"添加",
    //    buttonicon:"ace-icon fa fa-plus-circle purple",
    //    onClickButton: function(){
    //        openInsertPage("");
    //    },
    //    position:"last"
    //});

}

/**
* 打开新增页面
*/
function openInsertPage(id){
    window.location.href = springUrl+"/web/resourceInfo/insertPage?id="+id;
}

/**
* 打开修改页面
* @param id 主键ID
*/
function openUpdatePage(id){
    var element = document.createElement("input");
    element.setAttribute("type", "hidden");
    element.setAttribute("id", "id");
    element.setAttribute("name", "id");
    element.setAttribute("value", id);

    var form = document.createElement("form");
    document.body.appendChild(form);
    form.method = 'post';
    form.action = springUrl+'/web/resourceInfo/updatePage';
    form.appendChild(element);
    form.submit();
}

/**
* 根据主键删除数据
* @param id 主键ID
*/
function deleteByPrimaryKey(id){
     Commons.showConfirm("是否删除该记录?", function(){
         jQuery.ajax({
             type: "POST",
             dataType: "json",
             url: springUrl+"/web/resourceInfo/deleteByPrimaryKey",
             data: {
                 id:  id
             },
             success: function (data) {
                 Commons.showSuccess("数据删除成功!", function(){
                     $("#gridTable").trigger("reloadGrid");
                 });
             },
             error: function(data){
                 Commons.showError(data.responseText);
             }
         });
     })
}
